﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BookStore.Solution.BLL;
using BookStore.Solution.Entities;

namespace BookStore.Solution.Presentation
{
    public partial class ucStationery : UserControl
    {
        StationeryManager stationeryManager = new StationeryManager();
        public ucStationery()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FrmStationery frm = new FrmStationery();
            frm.ShowDialog();
            if (!frm.Visible)
            {
                LoadData();
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string productID = "";
            if (grvListofStationery.SelectedRows.Count > 0)
            {
                productID = grvListofStationery.CurrentRow.Cells["ProductID"].Value.ToString();
                FrmStationery frm = new FrmStationery(productID,false);
                frm.ShowDialog();
                if (!frm.Visible)
                {
                    LoadData();
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (grvListofStationery.SelectedRows.Count > 0)
            {
                DialogResult dlr = MessageBox.Show("Bạn có chắc chắn xóa những văn phòng phẩm đã chọn?", "Xác nhận xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                BookManager bookManager = new BookManager();
                string ProductID = "";
                List<string> del = new List<string>();
                if (dlr == DialogResult.Yes)
                {
                    for (int i = 0; i < grvListofStationery.SelectedRows.Count; i++)
                    {
                        ProductID = grvListofStationery.SelectedRows[i].Cells["ProductID"].Value.ToString();
                        if (bookManager.Delete(ProductID) == 0)
                        {
                            del.Add(ProductID);
                        }
                    }
                    if (del.Count != 0)
                    {
                        String list = "";
                        for (int i = 0; i < del.Count; i++)
                            list += "," + del[i].ToString();
                        MessageBox.Show("Xóa thất bại các văn phòng phẩm có mã: \n" + list, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Xóa thành công các văn phòng phẩm đã chọn", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    LoadData();
                }
            }
            else
            {
                MessageBox.Show("Lựa chọn ít nhất 1 văn phòng phẩm để xóa", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }

        }

        private void btnSearchbyStationeryCate_Click(object sender, EventArgs e)
        {
            FrmSearchbyStationeryCategory frm = new FrmSearchbyStationeryCategory();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Searched(frm.result);
            }
        }

        private void btnViewDetail_Click(object sender, EventArgs e)
        {
            string productID = "";
            if (grvListofStationery.SelectedRows.Count > 0)
            {
                productID = grvListofStationery.CurrentRow.Cells["ProductID"].Value.ToString();
                FrmStationery frm = new FrmStationery(productID, true);
                frm.ShowDialog();
            }
        }

        private void btnSeachbyStationeryID_Click(object sender, EventArgs e)
        {
            FrmSearchbyStationeryID frm = new FrmSearchbyStationeryID();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Searched(frm.result);
            }
        }

        private void btnSearchbyStationeryName_Click(object sender, EventArgs e)
        {
            FrmSearchbyStationeryName frm = new FrmSearchbyStationeryName();
            frm.ShowDialog();
            if (!frm.Visible && frm.result != null)
            {
                LoadData_Searched(frm.result);
            }
        }

        private void ucStationery_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        private void LoadData()
        {
            grvListofStationery.AutoGenerateColumns = false;
            grvListofStationery.DataSource = stationeryManager.GetAll();
            for (int i = 0; i < grvListofStationery.Columns.Count; i++)
            {
                grvListofStationery.Columns[i].DataPropertyName = grvListofStationery.Columns[i].Name;
            }
        }
        private void LoadData_Searched(List<Product> list)
        {
            grvListofStationery.AutoGenerateColumns = false;
            grvListofStationery.DataSource = list;
            for (int i = 0; i < grvListofStationery.Columns.Count; i++)
            {
                grvListofStationery.Columns[i].DataPropertyName = grvListofStationery.Columns[i].Name;
            }
        }

        private void tsmiRefesh_Click(object sender, EventArgs e)
        {
            LoadData();
        }
    }
}
